//
//  FLMAssistantViewController.m
//  FLMSuperS
//
//  Created by chenshan on 2016/10/9.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMAssistantViewController.h"

@interface FLMAssistantViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation FLMAssistantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kBackgroundColor;
    self.title = @"超级买助手";
    
    _textField  = [[UITextField alloc] initWithFrame:CGRectMake(10, NAVIGATIONBAR_HEIGHT + 5, self.view.width-50, 35)];
    _textField.font = [UIFont systemFontOfSize:12.0];
    _textField.placeholder = @"请输入问题或关键字";
    _textField.delegate = self;
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.tintColor = [UIColor blackColor];
    _textField.layer.cornerRadius  = 15.0;
    [self.view addSubview:_textField];
    
    _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(_textField.right + 5, _textField.top, 35, 35)];
    [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [_cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.view addSubview:_cancelBtn];
    
    _dataSource = @[].mutableCopy;
    
    [_dataSource addObject:@{
                             @"title" : @"新手教程",
                             @"show" : @"0",
                             @"subMenus" : @[@"如何认领订单",
                                             @"如何分享并截图上传凭证"]
                             }.mutableCopy];
    
    [_dataSource addObject:@{
                             @"title" : @"常见问题",
                             @"show" : @"0",
                             @"subMenus" : @[@"如何认领订单",
                                             @"如何分享并截图上传凭证"]
                             }.mutableCopy];
    
    [_dataSource addObject:@{
                             @"title" : @"频道说明",
                             @"show" : @"0",
                             @"subMenus" : @[@"如何认领订单",
                                             @"如何分享并截图上传凭证"]
                             }.mutableCopy];
    
    [self.view addSubview:self.tableView];
}

- (void)cancel:(UIButton *)sender {
    _textField.text = nil;
    [_textField resignFirstResponder];
}

#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dict = _dataSource[section];
    
    if([dict[@"show"] boolValue]) {
        return [dict[@"subMenus"] count];
    }
    
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSDictionary *dict = _dataSource[section];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 44)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [[UIButton alloc] initWithFrame:headerView.bounds];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tag = 1000 + section;
    [button addTarget:self action:@selector(menuClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:dict[@"title"] forState:UIControlStateNormal];
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, -1, headerView.width, 1)];
    topLine.backgroundColor = kBackgroundColor;
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, headerView.height - 1, headerView.width, 1)];
    bottomLine.backgroundColor = kBackgroundColor;
    
    [headerView addSubview:button];
    [headerView addSubview:topLine];
    [headerView addSubview:bottomLine];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = kBackgroundColor;
    
    NSDictionary *dict = _dataSource[indexPath.section];
    NSArray *subMenus = dict[@"subMenus"];
    
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = subMenus[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = _dataSource[indexPath.section];
    NSArray *subMenus = dict[@"subMenus"];
    
    NSString *title = subMenus[indexPath.row];
    
    //在这里根据title跳转
}

- (void)menuClicked:(UIButton *)sender {
    NSInteger section = sender.tag - 1000;
    
    NSMutableDictionary *dict = _dataSource[section];
    
    if([dict[@"show"] boolValue]) {
        dict[@"show"] = @"0";
    }
    else {
        dict[@"show"] = @"1";
    }
    
    [self.tableView reloadData];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

#pragma mark - Getters

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _textField.bottom + 5, ScreenW, self.view.height - _textField.bottom - 5) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kBackgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.rowHeight = 50.0;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    return _tableView;
}

@end
